<?php

namespace Home\Controller;

use Think\Controller;
use Think\Verify;

class PublicController extends Controller
{
    function _initialize()
    {
        if (!$_SESSION ["userInfo"]) {
            $this->redirect("Home/Login/index");
        }
    }


    public function verify(){
        $Verify =     new Verify();
        //对指定的变量参数赋值，调整验证码效果
        $Verify->fontSize = 30;
        $Verify->length   = 4;
        $Verify->useNoise = false;
        //图片输出
        $Verify->entry();
    }

    public function upload($files)
    {
        //是否有图片上传
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728; // 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = 'Public/'; // 设置附件上传根目录
        $upload->savePath = './Uploads/'; // 设置附件上传（子）目录
        // 上传文件
        $info = $upload->upload($files);

        if (!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        } else {// 上传成功 获取上传文件信息
            foreach ($info as $file) {
                //return $file['savepath'].$file['savename'];
            }
        }

        return $info;
    }

    public function uploadOne($file)
    {
        //是否有图片上传
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728; // 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/'; // 设置附件上传根目录
        $upload->savePath = './Uploads/'; // 设置附件上传（子）目录
        // 上传文件
        $info = $upload->uploadOne($file);

        if (!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        } else {// 上传成功 获取上传文件信息
            return $info;
        }

    }

    public function delpic($pic)
    {
        if (unlink(getcwd() . '/Public' . $pic)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @param $fileName
     * @param $headArr
     * @param $data 实例化phpexcel
     */
    public function getExcel($fileName, $headArr, $data)
    {
        //对数据进行检验
        if (empty($data) || !is_array($data)) {
            $this->error("导出失败,请确认有数据");
        }
        //检查文件名
        if (empty($fileName)) {
            exit;
        }
        $date = date("Y_m_d", time());
        $fileName .= "_{$date}.xls";
        //创建PHPExcel对象，注意，不能少了\
        $objPHPExcel = new \PHPExcel();
        $objProps = $objPHPExcel->getProperties();

        //设置表头
        $key = ord("A");
        foreach ($headArr as $v) {
            $colum = chr($key);
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $v);
            $key += 1;

        }
        $column = 2;
        $objActSheet = $objPHPExcel->getActiveSheet();

        foreach ($data as $key => $rows) { //行写入
            $span = ord("A");

            foreach ($rows as $keyName => $value) {// 列写入
                $j = chr($span);
                $objActSheet->setCellValue($j . $column, $value);
                $span++;

            }
            $column++;

        }
        $objPHPExcel->getActiveSheet()->getColumnDimension("A")->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension("B")->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension("C")->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension("D")->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension("F")->setAutoSize(true);
        $fileName = iconv("utf-8", "gb2312", $fileName);
        //重命名表
        // $objPHPExcel->getActiveSheet()->setTitle('test');
        //设置活动单指数到第一个表,所以Excel打开这是第一个表
        $objPHPExcel->setActiveSheetIndex(0);
        ob_end_clean();
        ob_start();
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename=\"$fileName\"");
        header('Cache-Control: max-age=0');
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output'); //文件通过浏览器下载
        exit;
    }

    /**
     * 导入
     */
    public function import($file)
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728; // 设置附件上传大小
        $upload->exts = array('xls', 'cvs', 'xlsx');// 设置附件上传类型
        $upload->rootPath = './Public/'; // 设置附件上传根目录
        $upload->savePath = 'Uploads/'; // 设置附件上传（子）目录
        // 上传文件
        $info = $upload->uploadOne($file);
        if (!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        } else {// 上传成功 获取上传文件信息
            import("Org.Util.PHPExcel");
            import("Org.Util.PHPExcel.Writer.Excel5");
            import("Org.Util.PHPExcel.IOFactory.php");
            $objRead = new \PHPExcel_Reader_Excel2007();   //建立reader对象
            chmod($upload->rootPath . $info['savepath'] . $info['savename'],0777);
            if ($objRead->canRead($upload->rootPath . $info['savepath'] . $info['savename'])) {
//                $objRead = new \ PHPExcel_Reader_Excel5();
                $obj = $objRead->load($upload->rootPath . $info['savepath'] . $info['savename']);  //建立excel对象
                $currSheet = $obj->getSheet(0);   //获取指定的sheet表
                $rowCount = $currSheet->getHighestRow();//excel行数
                $data = array();
                for ($i = 2; $i <= $rowCount; $i++) {
                    $data[]['mobile'] = $obj->getActiveSheet()->getCell("A" . $i)->getValue();
//                    $data[] = $obj->getActiveSheet()->getCell("B" . $i)->getValue();
//                    $data['login_uid'] = $obj->getActiveSheet()->getCell("B" . $i)->getValue();
//                    $data['login_time'] = $obj->getActiveSheet()->getCell("C" . $i)->getValue();
//                    $data['login_ip'] = $obj->getActiveSheet()->getCell("D" . $i)->getValue();


                }
                return $data;

            }else{
                die('没有上传excel');
            }
        }
    }
}